package cn.z2huo.demo.spring.transactional.service;

import cn.z2huo.demo.mapper.user.UserMapper;
import cn.z2huo.demo.model.dataobject.user.UserDO;
import cn.z2huo.demo.spring.transactional.dao.UserDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *
 * @author z2huo
 */
@Slf4j
@Service
public class ServiceB {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDAO userDAO;

    public void insertNoTransactional_insertRequired(UserDO userDO) {
        this.insertRequired(userDO);
    }

    public void insertNoTransactional(UserDO userDO) {
        userMapper.insert(userDO);
    }

    public void insertNoTransactionalException(UserDO userDO) {
        userMapper.insert(userDO);
        int a = 1/0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertRequired(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertRequiredException(UserDO userDO) {
        userMapper.insert(userDO);
        int a = 1 / 0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertRequiredExceptionCatch(UserDO userDO) {
        try {
            userMapper.insert(userDO);
            int a = 1 / 0;
        } catch (Exception e) {
            log.error("serviceB insert required exception, catch in B");
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertRequiredNew(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertRequiredNewException(UserDO userDO) {
        userMapper.insert(userDO);
        int a = 1 / 0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertRequiredNewExceptionCatch(UserDO userDO) {
        try {
            userMapper.insert(userDO);
            int a = 1 / 0;
        } catch (Exception e) {
            log.error("serviceB insert required new exception, catch in B");
        }
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void insertMandatory(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void insertNotSupported(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void insertNotSupportedException(UserDO userDO) {
        userMapper.insert(userDO);
        int a = 1/0;
    }

    @Transactional(propagation = Propagation.NEVER)
    public void insertNever(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void insertNested(UserDO userDO) {
        userMapper.insert(userDO);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void insertNestedException(UserDO userDO) {
        userMapper.insert(userDO);
        int a = 1/0;
    }

    @Transactional(propagation = Propagation.NESTED)
    public void insertNestedExceptionCatch(UserDO userDO) {
        try {
            userMapper.insert(userDO);
            int a = 1/0;
        } catch (Exception e) {
            log.error("serviceB insert nested exception, catch in B", e);
        }
    }


}
